package timbap.artwork.util;

import org.apache.commons.httpclient.methods.GetMethod;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class HttpGetBufferRegistry {

    private static HttpGetBufferRegistry INSTANCE;

    private Map<InputStream,GetMethod> httpGetBuffersToRelease = new HashMap<InputStream,GetMethod>();

    public void register(final InputStream pHttpGetStream, final GetMethod pGetMethod) {
        httpGetBuffersToRelease.put(pHttpGetStream, pGetMethod);
    }
    
    public void release(InputStream pInputStream) {
        GetMethod tGetMethodToRelease = httpGetBuffersToRelease.get(pInputStream);
        if (tGetMethodToRelease != null) {
            tGetMethodToRelease.releaseConnection();
            httpGetBuffersToRelease.remove(pInputStream);
        }
    }

    public static HttpGetBufferRegistry getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new HttpGetBufferRegistry();
        }
        return INSTANCE;
    }
}
